//
//  MessageStatus.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/31.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "MessageStatus.h"

MessageStatus *messageStatus;
@implementation MessageStatus
- (id)init{
    self = [super init];
    //如果生出自己就 呼叫loadDB
    if (self) {
        
    }
    //把自己回傳出去
    return self;
}

+(MessageStatus *)sharedInstance{
    
    if (messageStatus==nil) {
        
        messageStatus = [[MessageStatus alloc]init];
    }
    return messageStatus;
}

-(void)getMessageRecv:(id)data
{
    [MessageStatus changeData:data completionBlock:^(NSDictionary *result)
     {
         NSLog(@"%@",result);
         
         //取得版號相關資訊 OTASTATUS
         if ([[result objectForKey:@"type"]isEqualToString:@"OTASTATUS"])
         {
             //取得版本 使用 auto 來判斷 是更新前還是更新後
             if ([[result objectForKey:@"auto"]isEqualToString:@"0"]) {
                 
                 if (self.checkToyUpdateDelegate && [self.checkToyUpdateDelegate respondsToSelector:@selector(getCheckToyUpdateInfo:)])
                 {
                     [self.checkToyUpdateDelegate getCheckToyUpdateInfo:result];
                 }
             }else
             {
                 //更新完畢收到新的資料
                 if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusStart:)])
                 {
                     [self.toyUpdateMessageStatusDelegate toyUpdateFinishInfo:result];
                 }
             }
             
             
         }else if ([[result objectForKey:@"type"]isEqualToString:@"OTAUPDATE"])
         {
             if ([[result objectForKey:@"status"]isEqualToString:@"start"]) {
                 
                 NSLog(@"準備開始更新");
                 if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusStart:)])
                 {
                     [self.toyUpdateMessageStatusDelegate toyUpdateStatusStart:NSLocalizedString(@"準備開始更新", nil)];
                 }
                 
                 
             }else if ([[result objectForKey:@"status"]isEqualToString:@"0/0"]) {
                 
                 NSLog(@"開始更新");
                 if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusStarting:)])
                 {
                     [self.toyUpdateMessageStatusDelegate toyUpdateStatusStarting:NSLocalizedString(@"開始更新", nil)];
                 }
             }else if ([[result objectForKey:@"status"]isEqualToString:@"notstart"])
             {
                 
                 if ([[result objectForKey:@"reason"]isEqualToString:@"wrongtoy"]) {
                     
                     NSLog(@"wrongtoy : 娃娃有問題");
                     if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusError:)])
                     {
                         [self.toyUpdateMessageStatusDelegate toyUpdateStatusError:NSLocalizedString(@"娃娃有問題", nil)];
                     }
                 }else if([[result objectForKey:@"reason"]isEqualToString:@"wrongcreator"])
                 {
                     NSLog(@"wrongcreator : 使用者有問題");
                     if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusError:)])
                     {
                         [self.toyUpdateMessageStatusDelegate toyUpdateStatusError:NSLocalizedString(@"使用者有問題", nil)];
                     }
                 }
                 
             }else if ([[result objectForKey:@"status"]isEqualToString:@"-1/-1"])
             {
                 if ([[result objectForKey:@"reason"]isEqualToString:@"filenotfound"]) {
                     
                     NSLog(@"filenotfound : 檔案無作用");
                     if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusError:)])
                     {
                         [self.toyUpdateMessageStatusDelegate toyUpdateStatusError:NSLocalizedString(@"檔案無作用", nil)];
                     }
                 }else if([[result objectForKey:@"reason"]isEqualToString:@"nonewotaversion"]) {
                     
                     NSLog(@"nonewotaversion : 不是最新的版本");
                     if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusError:)])
                     {
                         [self.toyUpdateMessageStatusDelegate toyUpdateStatusError:NSLocalizedString(@"不是最新的版本", nil)];
                     }
                 }else if([[result objectForKey:@"reason"]isEqualToString:@"downloadfail"]) {
                     
                     NSLog(@"downloadfail : 下載失敗");
                     if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusError:)])
                     {
                         [self.toyUpdateMessageStatusDelegate toyUpdateStatusError:NSLocalizedString(@"下載失敗", nil)];
                     }
                 }
             }else if ([[result objectForKey:@"status"]isEqualToString:@"FILESIZE/FILESIZE"])
             {
                 if ([[result objectForKey:@"reason"]isEqualToString:@"checksumfail"]) {
                     
                     NSLog(@"checksumfail : 檢查檔案");
                     if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusError:)])
                     {
                         [self.toyUpdateMessageStatusDelegate toyUpdateStatusError:NSLocalizedString(@"檢查檔案", nil)];
                     }
                 }
             }else if ([[result objectForKey:@"status"]hasPrefix:@"-1"])
             {
                 if ([[result objectForKey:@"reason"]isEqualToString:@"timeout"]) {
                     
                     NSLog(@"timeout : 時間逾期");
                     if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusTimeOur:)])
                     {
                         [self.toyUpdateMessageStatusDelegate toyUpdateStatusTimeOur:NSLocalizedString(@"時間逾期", nil)];
                     }
                     // PS:after timeout case, next case will be  "Starting" again
                 }
             }else if ([[result objectForKey:@"status"]isEqualToString:@"md5start"])
             {
                 NSLog(@"md5start :md5開始");
                 if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusMD5Start:)])
                 {
                     [self.toyUpdateMessageStatusDelegate toyUpdateStatusMD5Start:NSLocalizedString(@"md5開始驗證", nil)];
                 }
             }else if ([[result objectForKey:@"status"]isEqualToString:@"md5success"])
             {
                 NSLog(@"md5success :md5 完成");
                 
                 if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusMD5Finish:)])
                 {
                     [self.toyUpdateMessageStatusDelegate toyUpdateStatusMD5Finish:NSLocalizedString(@"md5 完成/n等待娃娃自動啟動", nil)];
                 }
                 
             }else if ([[result objectForKey:@"status"]isEqualToString:@"md5fail"])
             {
                 NSLog(@"md5fail : md5 失敗");
                 
                 if ([[result objectForKey:@"reason"]isEqualToString:@"checksumerror"]) {
                     
                     NSLog(@"checksumerror : 檢查錯誤");
                     
                     if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusMD5Fail:)])
                     {
                         [self.toyUpdateMessageStatusDelegate toyUpdateStatusMD5Fail:NSLocalizedString(@"md5 失敗", nil)];
                     }
                 }
             }else
             {
                 
                 NSArray *sizeSplit = [[result objectForKey:@"status"] componentsSeparatedByString:@"/"];

                 NSLog(@"%@",sizeSplit);
                 
                 if (sizeSplit.count >0) {
                     
                     if (self.toyUpdateMessageStatusDelegate && [self.toyUpdateMessageStatusDelegate respondsToSelector:@selector(toyUpdateStatusProgress:total:received:)])
                     {
                         [self.toyUpdateMessageStatusDelegate toyUpdateStatusProgress:[result objectForKey:@"status"] total:[sizeSplit[1] floatValue] received:[sizeSplit[0] floatValue]];
                     }
                 }
             }
         }else if ([[result objectForKey:@"type"]isEqualToString:@"LOGINEDSTATUS"])
         {
             NSLog(@"%@",result);
         }
     }];
}

/*
 資料轉變 因為收到的訊息會是 key message_recv   :  值 messagestatus type=OTASTATUS auto=1 status=B hwver=2 runver=0.12.141211.526 downloadver=0.12.151228.220 所以要轉換
 */
+(void)changeData:(id)data completionBlock:(void(^)(NSDictionary* result))completion
{
    
    NSMutableDictionary *results = [@{}mutableCopy];
    
    NSArray *array = [data[@"message_recv"]componentsSeparatedByString:@" "];
    
    for (int i = 1 ; i <= array.count-1 ; i++) {
        
        NSArray *array2 = [array[i] componentsSeparatedByString:@"="];
        
        [results setObject:array2[1] forKey:array2[0]];
    }
    if (completion) {
        completion(results);
    }
}
@end
